import 'package:flutter/material.dart';

import 'Item.dart';
import 'Meow.dart';

class App extends StatefulWidget {
  
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {

  final _title = "猫叫模拟器";
  List<Widget> _children = List<Widget>.generate(0, (i)=>new Text(""));

  @override
  void initState() {
    super.initState();
    this.buildMeowItems();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text(_title),
        ),
        body: Container(
          child: GridView.count(
            crossAxisCount: 2,
            children: _children,
          ),
        )
      ),
    );
  }

  Future buildMeowItems() async {
    var queryMeows = await Meow.queryMeows();
    List<Widget> items= [];
    for (var meow in queryMeows) {
      var card = Item(meow: meow);
      items.add(card);
    }

    this.setState(() {
      this._children = items.toList();
    });
  }
}
